// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Text;
using System.Xml;

using QuoteAdvantage.CrmSdk;

namespace QuoteAdvantage
{
	/// <summary>
	/// Basic wrapper over the CRM quote object
	/// </summary>
   public class Quote 
   {
      private Guid m_QuoteId;
      private string m_strQuoteNumber;
      private string m_strName;
      private Guid m_PriceLevelId;
      private int m_nStateCode;
      private decimal nTotalAmount;
      private ArrayList m_LineItems = new ArrayList();

      public Quote()
      {
			
      }

      public ArrayList LineItems
      {
         get { return m_LineItems; }
      }
        
      public string Name
      {                                                    
         get { return m_strName; }
         set { m_strName = value; }
      }

      public Guid PriceLevelId
      {
         get { return m_PriceLevelId;}
         set { m_PriceLevelId = value;}
      }

      public Guid QuoteId
      {
         get { return m_QuoteId;}
         set { m_QuoteId = value;}
      }

      public string QuoteNumber
      {
         get { return m_strQuoteNumber; }
         set { m_strQuoteNumber = value; }
      }

      public int StateCode
      {
         get { return m_nStateCode;}
         set { m_nStateCode = value;}
      }

      public decimal TotalAmount
      {
         get { return nTotalAmount;}
         set { nTotalAmount = value;}
      }

      // Add up all line item prices without discounts
      public decimal CalculateNonDiscountPrice()
      {
         decimal nTotal = 0;

         foreach (QuoteDetail objItem in LineItems)
         {
            nTotal += (objItem.PricePerUnit * objItem.Quantity);
         }
      
         return nTotal;
      }   

      public decimal CalculatePrice()
      {
         decimal nTotal = 0;

         foreach (QuoteDetail objItem in LineItems)
         {
            decimal nPrice = objItem.PricePerUnit * objItem.Quantity;
            nPrice = nPrice - objItem.ManualDiscountAmount;
            nTotal += nPrice;
         }
      
         return nTotal;
      }   

      // Retrieve all items on this quote that are root level, configurable items
      public ArrayList GetConfigurableItems()
      {
         ArrayList   resultList = new ArrayList(10);

         foreach (QuoteDetail item in this.LineItems)
         {
            if (item.ConfigParentId == Guid.Empty)
            {
               resultList.Add(item);
            }                       
         }
         
         return resultList;   
      }

      public QuoteDetail FindQuoteDetail(Guid guidQuoteDetailId)
      {
         QuoteDetail    objResultItem = null;
     
         foreach (QuoteDetail objItem in this.LineItems)
         {
            if (objItem.QuoteDetailId == guidQuoteDetailId)
            {   
               objResultItem = objItem;
               break;
            }
         }

         return objResultItem;
      }

      public QuoteDetail FindQuoteDetail(string strProductNumber, string strCategoryId)
      {
         QuoteDetail objResultItem = null;
         string strTargetProductNumber  = strProductNumber.ToUpper();
    
         foreach (QuoteDetail objItem in this.LineItems)
         {
            if (objItem.ProductNumber.ToUpper() == strTargetProductNumber && objItem.CategoryId == strCategoryId)
            {   
               objResultItem = objItem;
               break;
            }
         }

         return objResultItem;
      }


      // Search an item's quote details for a line item with a specific category id
      public QuoteDetail FindCategoryItem(QuoteDetail objParentItem, string strCategoryId)
      {
         QuoteDetail objResultItem = null;
         string strTargetCategory  = strCategoryId.ToUpper();

         foreach (QuoteDetail objItem in this.LineItems)
         {
            if (objItem.ConfigParentId == objParentItem.QuoteDetailId)
            {
               if (objItem.CategoryId.ToUpper() == strTargetCategory)
               {   
                  objResultItem = objItem;
                  break;
               }
            }
         }

         return objResultItem;
      }


      // Search an item's quote details for a line item with a specific category id and a specific Product Number
      public QuoteDetail FindCategoryItem(QuoteDetail objParentItem, string strCategoryId, string strProductNumber)
      {
         QuoteDetail objResultItem = null;
         string strTargetCategory  = strCategoryId.ToUpper();
         string strTargetProductNumber  = strProductNumber.ToUpper();
     
         foreach (QuoteDetail objItem in this.LineItems)
         {
            if (objItem.ConfigParentId == objParentItem.QuoteDetailId)
            {
               if (objItem.CategoryId.ToUpper() == strTargetCategory && objItem.ProductNumber.ToUpper() == strTargetProductNumber)
               {   
                  objResultItem = objItem;
                  break;
               }
            }
         }

         return objResultItem;
      }

      public void LoadFromCrmQuote(quote objCrmQuote)
      {
         this.Name = objCrmQuote.name;
         this.QuoteId = objCrmQuote.quoteid.Value;
         this.QuoteNumber = objCrmQuote.quotenumber;
         this.PriceLevelId = objCrmQuote.pricelevelid.Value;
         this.StateCode = (int)objCrmQuote.statecode.Value;
         this.TotalAmount = objCrmQuote.totalamount.Value;
      }   

      public quote CreateCrmObject()
      {
         quote objCrmQuote = new quote();

         objCrmQuote.quoteid = new Key();
         objCrmQuote.quoteid.Value = this.QuoteId;

         objCrmQuote.totallineitemamount = new CrmMoney();
         objCrmQuote.totallineitemamount.Value = this.CalculatePrice();

         return objCrmQuote;
      }

      public bool IsReadOnly
      {
         get 
         {
            if (this.m_nStateCode != 0)
               return true;
            else
               return false;
         }
      }
     
      public void LoadQuoteDetails(BusinessEntityCollection objItemList)
      {
         this.LineItems.Clear();

         foreach (BusinessEntity objCrmItem in objItemList.BusinessEntities)
         {
            quotedetail objCrmQuoteDetail = (quotedetail)objCrmItem;
            QuoteDetail objLineItem = new QuoteDetail();
            objLineItem.LoadFromCrmQuoteDetail(objCrmQuoteDetail, this);
            this.LineItems.Add(objLineItem);
         }
      }

    

	} // end class
}
